package com.zp.self.module.工具.json转换.json调整;

import com.zp.self.module.工具.json转换.jsonUtil.JsonUtil;
import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;

/**
 * @author ：by peng.zeng
 * @date ：Created 2021/9/3 15:05
 * @description ：整理
 */
public class Json_抽取必要属性_新模型id {
    public static void main(String[] args) throws IOException {
        JSONArray mainJson = JsonUtil.getJson("C:\\Users\\peng.zeng\\Desktop\\DeviceAll_20220310.json");
        JSONArray anew = new JSONArray();
        for (int i = 0; i < mainJson.length(); i++) {
            try {
                JSONObject json = mainJson.getJSONObject(i);
                JSONObject jsonNew = new JSONObject();

                jsonNew.put("modelId",json.getJSONObject("type").get("deviceTypeId"));
                jsonNew.put("thingId", json.get("id"));
                jsonNew.put("deviceId", json.get("assetId"));

                anew.put(jsonNew);
            } catch (JSONException e) {
                System.out.println("fail:"+mainJson.getJSONObject(i).toString());
            }
        }

        String outPathStr = "C:\\Users\\peng.zeng\\Desktop\\DeviceAll_thingId_20220310.json";
        File file = new File(outPathStr);
        FileUtils.writeStringToFile(file, anew.toString(), "utf-8", true);
//        JsonUtil.json2csv(mainJson,"C:\\Users\\peng.zeng\\Desktop\\2022-03-01 14::00.csv");
    }
}
